<?php
/**
 * 
 * @author Youssef Driouach
 *
 * 20 août 2012
 */
class IndexController extends Zend_Controller_Action
{

	protected $token;
	
    public function init()
    {
    	$script = "$(function(){
    	
			$('#select').change(function() {
				$('#div_surface, #div_mileage, #div_transmission, #div_year, #div_energy, #div_cylinder').hide();
				if($(this).find('option:selected').attr('value') == 'car'){
			   		$('#div_mileage, #div_transmission, #div_year, #div_energy').show();
				}
				if($(this).find('option:selected').attr('value') == 'motorcycle'){
			   		$('#div_mileage, #div_cylinder, #div_year').show();
				}
				if($(this).find('option:selected').attr('value') == 'house' || $(this).find('option:selected').attr('value') == 'apartment'){
					
			   		$('#div_surface').show();
				}
		   		
			});
			$('.btn-group .btn').click(function() {
			    // whenever a button is clicked, set the hidden helper
			    $('#class').val($(this).text());
			}); 
			$(document).ready(function() {
				$('#francemap').vectorMap({
				    map: 'france_fr',
					hoverOpacity: 0.5,
					hoverColor: false,
					backgroundColor: '#ffffff',
					colors: couleurs,
					borderColor: '#000000',
					selectedColor: '#EC0000',
					enableZoom: true,
					showTooltip: true,
				    onRegionClick: function(element, code, region)
				    {
				        var message = '\"Département : \"'
				            + region 
				            + '\" || Code : \"'
				            + code
							+ '\"';
		             
				        $('#localisation').val(code);
				    }
				});
			});
    	
		})";
    	
    	$this->view->headScript()->appendScript($script, $type = 'text/javascript');
        /* Initialize action controller here */
    	$baseurl = $this->getFrontController()->getBaseUrl();
     	$this->view->headLink()->appendStylesheet($baseurl."/france_jqvmap/jqvmap.css");
    	$this->view->headScript()->appendFile( $baseurl."/france_jqvmap/jquery.vmap.js", $type = 'text/javascript' );
    	$this->view->headScript()->appendFile( $baseurl."/france_jqvmap/jquery.vmap.france.js", $type = 'text/javascript' );
    	$this->view->headScript()->appendFile( $baseurl."/france_jqvmap/jquery.vmap.colorsFrance.js", $type = 'text/javascript' );
    }
    
    public  function preDispatch(){
    	
    	$token = new Zend_Session_Namespace('token');
    	$token->delete = md5(time()*rand(1,10));
    	$this->token = $token;
    
    }

    public function indexAction()
    {
        // action body
        $auth = Zend_Auth::getInstance();
        $this->view->connected = false;
    	if($auth->hasIdentity()){
    		$form = new Application_Form_Search('search');
    		$request = new Application_Model_Request();
    		$reqMapper = new Application_Model_RequestMapper();
    		$reqBuilder = new Application_Model_Builder_Request();
    		$userMapper = new Application_Model_UserMapper();
    		$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
    		$requests = null;
    		$lastRequests = null;
    		if($this->_request->isPost()){
    			$formData = $this->getRequest()->getPost();
    			if($form->isValid($formData))
    			{
    				
    				$reqBuilder->setForm($form);
    				$request = $reqBuilder->createRequest();
    				$request->setUser($userConnected);
    				$reqMapper->save($request);
    				$this->_helper->redirector('index');
    			}
    			
    			
    			
			}
			$where = array('id_user = ?' => $userConnected->getUserId());
			$requests = $reqMapper->findAll($where,array('date DESC'));
			$lastRequests = $reqMapper->findAll(null,array('date DESC'),5);
			
			$offerMapper = new Application_Model_OfferMapper();
			//@TODO replace priceCurrency by avalability
			$where = array("priceCurrency = ?" => "€");
			$offers = $offerMapper->fetchAll($where,"startDate DESC",5);
				
			$this->view->offers = $offers;
			
			if(0 == count($offers))
				$this->view->message = 'No object yet, please add new objects';
			
			///////////////////////////////
			
			$this->view->form = $form;
			$this->view->lastrequests = $lastRequests;
			$this->view->requests = $requests;
			$this->view->token = $this->token;
			$this->view->connected = true;
			
    	}else{
    		$this->view->connected = false;
    	}
    	
    	
    }


}

